package com.hbusa.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import cn.hutool.db.sql.Direction;
import cn.hutool.db.sql.Order;
import com.hbusa.dao.UserDao;
import com.hbusa.dto.TbUser;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author XiaoXin
 * @date 2021/12/23 - 17:27
 */


public class UserDaoImpl implements UserDao {
    @Override
    public boolean save(TbUser user) {
        int insert = 0;
        try {
            insert = Db.use().insert(
                    Entity.create("tb_user")
                            .set("uname", user.getUname())
                            .set("upwd", user.getUpwd())
                            .set("role", user.getRole())
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return insert > 0;
    }

    // uppwd tb_user set name=?,upwd=? where uid=?
    @Override
    public boolean modify(TbUser user) throws SQLException {
        int update = Db.use().update(
                Entity.create()
                        .set("uname", user.getUname())
                        .set("upwd", user.getUpwd())
                        .set("role", user.getRole()),
                Entity.create("tb_user")
                        .set("uid", user.getUid())
        );
        return  update > 0;
    }

    @Override
    public boolean removeById(int id) {
        int uid = 0;
        try {
            uid = Db.use().del(
                    Entity.create("tb_user")
                            .set("uid", id)
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return uid > 0;
    }

    @Override
    public TbUser queryById(int id) {
        List<TbUser> user = null;
        try {
            user = Db.use().find(
                    Entity.create("tb_user")
                            .set("uid", id), TbUser.class
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user != null && user.size() > 0 ? user.get(0): null;
    }

    @Override
    public TbUser queryByUsernameAndPassword(String username, String password) throws SQLException {
        List<TbUser> user = null;
        List<TbUser> users = Db.use().find(
                Entity.create("tb_user")
                        .set("uname", username)
                        .set("upwd", password), TbUser.class
        );
        return users != null && users.size() > 0 ? users.get(0): null;
    }

    /**
     *
     * @param pageindex 当前页
     * @param pagesize  每页显示的条目数
     * @param searchMap 查询的条件
     * @return
     */
    @Override
    public PageResult<Entity> queryByPage(int pageindex, int pagesize, Map<String, Object> searchMap) {
        // 查询条件
        Entity tb_user = Entity.create("tb_user");
        if (searchMap != null && searchMap.size() > 0)
        {
            Set<Map.Entry<String, Object>> entries = searchMap.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                Object value = entry.getValue();
                tb_user.set(key, value);
            }
        }

        // 分页查询
        PageResult<Entity> page = null;
        try {
            Page pageEntity = new Page(pageindex, pagesize);
            pageEntity.addOrder(new Order("uid", Direction.DESC));
            page = Db.use().page(
                    tb_user, pageEntity);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return page;
    }

    public static void main(String[] args) throws SQLException {
        UserDaoImpl userDao = new UserDaoImpl();
        //userDao.save(new TbUser(0, "xiaohei", "456789", "普通用户"));
        // userDao.save(new TbUser(0, "xiaorao", "123654", "普通用户"));
        // userDao.save(new TbUser(0, "xiaokang", "987654", "普通用户"));

        // PageResult<Entity> entities = new UserDaoImpl().queryByPage(1, 2, null);
        // System.out.println(entities.getTotal()); // 总共有多少条数据
        // System.out.println(entities.getPageSize()); // 每页结果数
        // System.out.println(entities.getPage()); // 当前页下标
        // System.out.println(entities.getTotalPage()); // 总页数
        //
        // for (int i = 0; i < entities.size(); i++) {
        //     System.out.println(entities.get(i));
        // }

        // TbUser tbUser = userDao.queryById(1);
        // System.out.println(tbUser);

//         try {
//             userDao.modify(new TbUser(2, "小白", "666666", "管理员"));
//         } catch (SQLException throwables) {
//             throwables.printStackTrace();
//         }

        // boolean b = userDao.removeById(4);
        // if (b) {
        //     System.out.println("删除成功");
        // } else {
        //     System.out.println("删除失败");
        // }
        TbUser tbUser = userDao.queryByUsernameAndPassword("xiaoxin", "123456");
        System.out.println(tbUser);
    }
}
